<?php
            
use app\model\Util; 
use app\model\Theme; 


/**
 * Author: cbw
 * Desc: 抖音商城项目
 * Time: 2021/12/14 1:46 下午
 * Email: 17854288794@163.com
 */

class Info_Page extends PluginWebPage
{

    public function main()
    {
        global $_G,$_P,$_S,$_C;
        $action = 11;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }

        $type = isset($_P['type'])?$_P['type']:0;
        if (empty($_P['createtime']['start'])){
            $start_day =db_fetchcolumn ( 'SELECT min(created_at) FROM ' . tablename ( t_shangjia_company_info ).'where themeid='.$themeid );
            $starttime =  strtotime($start_day);
        }else{
            $starttime = strtotime($_P['createtime']['start']);
            $start_day = date("Y-m-d H:i:s",$_P['createtime']['start']);
        }
        $endtime = !$_P['createtime']['end']?time():strtotime($_P['createtime']['end']);
        $end_day = empty($_P['createtime']['end']) ? date('Y-m-d H:i:s',time()): date("Y-m-d H:i:s",$_P['createtime']['end']) ;

        $search_data = isset($_P['search_data'])?$_P['search_data']:'';
        if ($search_data){
            $search_data = '%'.$search_data.'%';
        }
        $page_index = max(1,intval($_P['page']));
        $page_size = 50;
        $page_start = ($page_index-1)*$page_size;

        $type_arr = BeilaiModel::getCompanyTypeCount($themeid);
        $page_total = BeilaiModel::getCompanyInfoList($themeid,$page_start,$page_size,'','','',$type,true);
        $list = BeilaiModel::getCompanyInfoList($themeid,$page_start,$page_size,$start_day,$end_day,$search_data,$type);

        $total = $page_total;
        //导出礼品记录
        if (checksubmit('export_submit', true)){
            $count = $total;
            $page_limit = 2000;
            $pagesize = ceil($count/$page_limit);
            $header = array(
                'type' => '申请类型', 'company_name' => '机构名称', 'username' => '负责人姓名',
                'address' => '所在城市','mobile' => '手机号',  'created_at'=>'填写时间'
            );
            $keys = array_keys($header);
            $html = "\xEF\xBB\xBF";
            foreach ($header as $li) {
                $html .= $li . "\t ,";
            }
            $html .= "\n";
//            for ($j = 1; $j <= $pagesize; $j++) {
                $condition=" appid=".$_G['platform']['appid']." ";

                if ($search_data){
                    $condition .= " AND mobile LIKE '".$search_data."'"." or username LIKE '".$search_data."'"." or company_name LIKE '".$search_data."'"." or address LIKE '".$search_data."'";
                }
                if ($start_day && $end_day && $start_day!=$end_day){
                    $condition .= "  and created_at between '".$start_day."' and '".$end_day."'";
                }
                if ($type){
                    $condition .= "  and type=".$type;
                }

                $ex_page = $_P['ex_page']?$_P['ex_page']:1;

                $sql = " SELECT * FROM "
                    .tablename(t_shangjia_company_info).
                    " WHERE  ".$condition.
                    "  order by created_at desc limit ". ($ex_page - 1) * $pagesize . ",$pagesize ";

                $list = db_select($sql);

                if (!empty($list)) {
                    $size = ceil(count($list) / 500);
                    for ($i = 0; $i < $size; $i++) {
                        $buffer = array_slice($list, $i * 500, 500);
                        foreach ($buffer as $row) {
                            switch ($row['type']){
                                case 1:
                                    $row['type'] = '机构';
                                    break;
                                case 2:
                                    $row['type'] = '城市服务商';
                                    break;
                                default:
                                    break;
                            }

                            foreach ($keys as $key) {
                                $data[] = $row[$key];
                            }
                            if (isset($data)){
                                $user[] = implode("\t ,", $data) . "\t ,";
                                unset($data);
                            }
                        }
                        $html .= implode("\n", $user)."\n";
                        unset($user);
                    }
                }
//            }
            $is_end = 0;
            if($ex_page>=$pagesize){
                $is_end = 1;
            }
            //新版本导出合并
            Util::WxJump($_S,$html,'商家登录信息',$themeid,$is_end,$pagesize);
        }
        $pager = pagination($page_total,$page_index,$page_size);
        $return_clicent = get_defined_vars();
return $this->view('/beilai/info/main', $return_clicent);
    }
}